package com.shjzit.loglib.db;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.shjzit.loglib.db.dao.LogDao;
import com.shjzit.loglib.db.entity.LogEntity;

@Database(entities = {LogEntity.class}, version = LocalDatabase.DATABASE_VERSION, exportSchema = false)
public abstract class LocalDatabase extends RoomDatabase {
    private static final String TAG = "LocalDatabase";
    public static final String DATABASE_NAME = "android_logcat.db";

    public static final int DATABASE_VERSION = 2;


    public abstract LogDao logDao();


    ////////////////////////////////////////////////////////////////////////////////////////////////
    //  数据库操作：升级
    ////////////////////////////////////////////////////////////////////////////////////////////////

    public static LocalDatabase create(final Context context) {
        return Room.databaseBuilder(
                        context,
                        LocalDatabase.class,
                        DATABASE_NAME)
                //崩溃后重建
                .fallbackToDestructiveMigration()
                //允许主线程访问数据库
                .allowMainThreadQueries()
                .build();
    }
}
